package com.app.buss.ui;

import android.os.Bundle;
import android.widget.TextView;

import com.app.buss.R;
import com.app.buss.base.BaseActivity;
import com.app.buss.bean.LlineBean;
import com.app.buss.bean.MarketBean;
import com.app.buss.mvp.presenter.MainPresenter;
import com.app.buss.mvp.view.IMainView;
import com.vinsonguo.klinelib.chart.KLineView;
import com.vinsonguo.klinelib.chart.TimeLineView;
import com.vinsonguo.klinelib.model.HisData;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KLineDetailActivity extends BaseActivity<MainPresenter> implements IMainView {


    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_add_num)
    TextView tvAddNum;
    @BindView(R.id.tv_add_rate)
    TextView tvAddRate;
    @BindView(R.id.tv_high_num)
    TextView tvHighNum;
    @BindView(R.id.tv_low_num)
    TextView tvLowNum;
    @BindView(R.id.timeline)
    KLineView timeline;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected int bindContent() {
        return R.layout.aty_kline_detail;
    }

    @Override
    protected void initSome() {
        String code = getIntent().getExtras().getString("name");
        String title = getIntent().getExtras().getString("title");
        initToolBar(title);
        mPresenter.getKline(this,code);
    }

    @Override
    public void getDataSuccess(Object model) {
        if(model instanceof LlineBean){
            LlineBean bean = (LlineBean) model;
            if(bean.getData()!=null && bean.getData().size()>0){
                tvNum.setText(bean.getData().get(0).getClose()+"");
                tvHighNum.setText(bean.getData().get(0).getHigh()+"");
                tvLowNum.setText(bean.getData().get(0).getLow()+"");
                if(bean.getData().get(0).getClose()-bean.getData().get(0).getOpen()>0){
                    tvNum.setTextColor(getResources().getColor(R.color.green));
                    tvAddNum.setText("+"+(bean.getData().get(0).getClose()-bean.getData().get(0).getOpen()));
                    tvAddNum.setTextColor(getResources().getColor(R.color.green));
                    tvAddRate.setTextColor(getResources().getColor(R.color.green));
                    tvAddRate.setText("+5.5%");
                }else {
                    tvNum.setTextColor(getResources().getColor(R.color.red));
                    tvAddNum.setText(bean.getData().get(0).getClose()-bean.getData().get(0).getOpen()+"");
                    tvAddNum.setTextColor(getResources().getColor(R.color.red));
                    tvAddRate.setTextColor(getResources().getColor(R.color.red));
                    tvAddRate.setText("-5.5%");
                }
                List<HisData> hisDatas = new ArrayList<>();
                for(LlineBean.DataBean bean1 : bean.getData()){
                    hisDatas.add(new HisData(bean1.getOpen(),bean1.getClose(),
                            bean1.getHigh(),bean1.getLow(),bean1.getVol().intValue(),bean.getTs()));
                }
                timeline.setDateFormat("HH:mm");
                timeline.setLastClose(hisDatas.get(0).getClose());
                timeline.initData(hisDatas);
                timeline.showKdj();
            }
        }
    }

    @Override
    public void getDataFail(String msg) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

}
